import {ref, computed} from 'vue'
import {defineStore} from 'pinia'
import {userInfoApi, userLogoutApi} from "@/api/user_api";
import {Message} from "@arco-design/web-vue";
import {parseToken} from "@/utils/parse_token";
import router from "@/router";
import type {siteInfoType} from "@/api/settings_api";
import {getSiteConfigApi} from "@/api/settings_api";

interface userInfoType {
    userID: number
    nickName: string
    avatar: string
    role: number
    token: string
}

interface userStoreType {
    userInfo: userInfoType
    siteInfo: siteInfoType
    chat: {
        content: string
        imageList: string[]
    }
}


export const userStorei = defineStore('userStore', {
    state: (): userStoreType => {
        return {
            userInfo: {
                userID: 0,
                nickName: "",
                avatar: "",
                role: 0,
                token: "",
            },
            siteInfo: {
                "site": {
                    "title": "木子AI",
                    "enTitle": "muziai",
                    "logo": "",
                    "slogan": "muziai",
                    "abstract": "123",
                    "beian": "1111"
                },
                "project": {
                    "title": "木子AI",
                    "icon": "",
                    "webPath": "uploads/index.html"
                },
                "seo": {
                    "keywords": "111",
                    "description": "1111"
                },
                "register": {
                    "isEmailRegister": true,
                    "isWxRegister": true,
                    "isSmsRegister": true
                },
                "login": {
                    "isUsernameLogin": true,
                    "isEmailLogin": true,
                    "isTelLogin": true,
                    "userSimultaneousLoginCount": 3
                }
            },
            chat: {
                content: "",
                imageList: []
            }
        }
    },
    actions: {
         saveUserInfo(token: string) {
            // 传一个token过来，然后重新去调用户信息接口
            this.userInfo.token = token
             userInfoApi().then(res=>{
                if (res.code) {
                    Message.error(res.msg)
                    return
                }
                 this.userInfo = {
                     userID: res.data.userID,
                     nickName: res.data.nickname,
                     avatar: res.data.avatar,
                     role: res.data.role,
                     token: token,
                 }
                 // 持久化
                 localStorage.setItem("userInfo", JSON.stringify(this.userInfo))

            })
        },
        loadUserInfo() {
            const val = localStorage.getItem("userInfo")
            if (!val) {
                return
            }
            try {
                this.userInfo = JSON.parse(val)
            } catch (e) {
                console.log(e)
                console.log(val)
                return;
            }

            // 判断token有没有过期
            const payLoad = parseToken(this.userInfo.token) // exp的时间是秒
            const nowTime = new Date().getTime() // 单位是毫秒
            if (payLoad.exp * 1000 - nowTime <= 0) {
                Message.warning("token已过期")
                localStorage.removeItem("userInfo")
                router.push({name: "web"})
                return;
            }
        },
        async userLogout() {
            const res = await userLogoutApi()
            localStorage.removeItem("userInfo")
            this.userInfo = {
                userID: 0,
                nickName: "",
                avatar: "",
                role: 0,
                token: "",
            }
            Message.success("用户注销成功")
            router.push({name: "webIndex"})
        },

        async getSite(){
            const res = await getSiteConfigApi("site_info")
            console.log(res)
            this.siteInfo = res.data
        }
    },
    getters: {
        isLogin(): boolean {
            return this.userInfo.userID !== 0
        },
        isAdmin(): boolean {
            return this.userInfo.role == 1
        }
    }
})
